package org.jeecg.modules.phone.controller;

import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.ss.formula.functions.T;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.modules.aliyun.phone.service.IPhoneService;
import org.jeecg.modules.aliyun.phone.service.ITaskService;
import org.jeecg.modules.phone.entity.*;
import org.jeecg.modules.phone.enums.TaskEnums;
import org.jeecg.modules.phone.mapper.PhoneAccountMapper;
import org.jeecg.modules.phone.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;

import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import static java.lang.Thread.sleep;

/**
 * @Description: 账户管理
 * @Author: jeecg-boot
 * @Date:   2022-08-11
 * @Version: V1.0
 */
@Slf4j
@Api(tags="账户管理")
@RestController
@RequestMapping("/phone/phoneAccount")
public class PhoneAccountController extends JeecgController<PhoneAccount, IPhoneAccountService> {
	@Autowired
	private IPhoneAccountService phoneAccountService;
	@Autowired
	private IPhoneAccountLogService phoneAccountLogService;
	@Autowired
	private IPhoneInstancesService phoneInstancesService;
	@Autowired
	private IPhoneTaskService phoneTaskService;
	@Autowired
	private IPhoneAccountTaskService phoneAccountTaskService;
	@Autowired
	ITaskService taskService;
	
	/**
	 * 分页列表查询
	 *
	 * @param phoneAccount
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "账户管理-分页列表查询")
	@ApiOperation(value="账户管理-分页列表查询", notes="账户管理-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(PhoneAccount phoneAccount,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<PhoneAccount> queryWrapper = QueryGenerator.initQueryWrapper(phoneAccount, req.getParameterMap());
		Page<PhoneAccount> page = new Page<PhoneAccount>(pageNo, pageSize);
		IPage<PhoneAccount> pageList = phoneAccountService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 * 添加
	 *
	 * @param phoneAccount
	 * @return
	 */
	@AutoLog(value = "账户管理-添加")
	@ApiOperation(value="账户管理-添加", notes="账户管理-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody PhoneAccount phoneAccount) {
		phoneAccountService.save(phoneAccount);
		return Result.OK("添加成功！");
	}
	
	/**
	 * 编辑
	 *
	 * @param phoneAccount
	 * @return
	 */
	@AutoLog(value = "账户管理-编辑")
	@ApiOperation(value="账户管理-编辑", notes="账户管理-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<?> edit(@RequestBody PhoneAccount phoneAccount) {
		phoneAccountService.updateById(phoneAccount);
		return Result.OK("编辑成功!");
	}

//
//	 /**
//	  * 创建登录盘
//	  * 步骤：用公司母盘创建手机。打开抖音。登录。打卡猫，登录，等待验证码，登录，保存登录盘
//	  * @param phoneAccount
//	  * @return
//	  */
//	 @AutoLog(value = "账户管理-创建登录盘")
//	 @ApiOperation(value="账户管理-创建登录盘", notes="账户管理-创建登录盘")
//	 @RequestMapping(value = "/createLoginImg", method = {RequestMethod.PUT,RequestMethod.POST})
//	 public Result<?> createLoginImg(@RequestBody PhoneAccount phoneAccount) {
//		 phoneAccountService.updateById(phoneAccount);
////		 RunCommandResponseBody
//		 return Result.OK("编辑成功!");
//	 }
//
//	 @AutoLog(value = "账户管理-公司母盘")
//	 @ApiOperation(value="账户管理-公司母盘", notes="账户管理-公司母盘")
//	 @RequestMapping(value = "/createAppImg", method = {RequestMethod.PUT,RequestMethod.POST})
//	 public Result<?> createAppImg() {
////		 一次性，用完可以删除，步骤：开一个常规手机， 发送安装命令。执行一次。保存为公司母盘
//
//
//		 String instances = phoneService.doRunInstances(new PhoneOrderPamarsVo()
//				 .setAmount(1)
//				 .setImageId("android_9_0_0_release_3285106_20220531.raw")
//
////            cpm-bp14thtbvv8k0u6uqm9p   公司母盘
//				 .setChargeType("PostPaid")
//				 .setDescription("公司母盘")
//				 .setInstanceName("companyImg")
//				 .setInstanceType("ecp.ce.large")
//				 .setKeyPairName("homePhone")
//				 .setRegionId("cn-hangzhou")
//				 .setSecurityGroupId("sg-bp1ggdbov2o3nvybkoey")
//				 .setVSwitchId("vsw-bp1f5diq3vbh9dw4frryq")
//
//		 );
//
//		 String instanceId = JSON.parseObject(instances).getJSONObject("instanceIds")
//				 .getJSONArray("instanceId").get(0).toString();
//
//		 return Result.OK(instanceId);
//	 }
//
//	@AutoLog(value = "账户管理-公司母盘")
//	@ApiOperation(value="账户管理-公司母盘", notes="账户管理-公司母盘")
//	@RequestMapping(value = "/remote", method = {RequestMethod.PUT,RequestMethod.POST})
//	public Result<?> remote( @RequestBody UploadFilePamarsVo uploadFilePamarsVo) {
//
//		String instanceId = uploadFilePamarsVo.getInstanceId();
//
//		//获取远程地址，开始远程控制
//		String vncUrlResult = phoneService.doListInstanceVncUrl(new UploadFilePamarsVo()
//				.setInstanceId(instanceId)
//				.setRegionId("cn-hangzhou")
//		);
//		String vncUrl = JSON.parseObject(vncUrlResult).getString("vncUrl");
//		return Result.OK("https://g.alicdn.com/aliyun/ecs-console-vnc2/0.0.8/index.html?vncUrl="
//				+ vncUrl + "&instanceId=" + instanceId  + "&password=ABcd12" )  ;
//
//
//	}
//	@AutoLog(value = "账户管理-公司母盘")
//	@ApiOperation(value="账户管理-公司母盘", notes="账户管理-公司母盘")
//	@RequestMapping(value = "/installApp1", method = {RequestMethod.PUT,RequestMethod.POST})
//	public Result<?> installApp1( @RequestBody UploadFilePamarsVo uploadFilePamarsVo) {
//
//		String instanceId = uploadFilePamarsVo.getInstanceId();
//		String douYin = phoneService.doInstallApplication(new UploadFilePamarsVo()
//				.setInstanceId(instanceId)
//				.setOssBucket("ylphone")
//				.setOssObject("apk/21.3.apk")
//				.setRegionId("cn-hangzhou")
//		);
//		return Result.OK(douYin)  ;
//	}
//
//	@AutoLog(value = "账户管理-公司母盘")
//	@ApiOperation(value="账户管理-公司母盘", notes="账户管理-公司母盘")
//	@RequestMapping(value = "/installApp2", method = {RequestMethod.PUT,RequestMethod.POST})
//	public Result<?> installApp2( @RequestBody UploadFilePamarsVo uploadFilePamarsVo) {
//
//		String instanceId = uploadFilePamarsVo.getInstanceId();
//		String douMao = phoneService.doInstallApplication(new UploadFilePamarsVo()
//				.setInstanceId(instanceId)
//				.setOssBucket("ylphone")
//				.setOssObject("apk/com.doutui.bozhong_2.5.6_100.apk")
//				.setRegionId("cn-hangzhou")
//		);
//
//		return Result.OK(douMao)  ;
//	}
//
//
//	@AutoLog(value = "账户管理-公司母盘")
//	@ApiOperation(value="账户管理-公司母盘", notes="账户管理-公司母盘")
//	@RequestMapping(value = "/saveImg", method = {RequestMethod.PUT,RequestMethod.POST})
//	public Result<?> saveImg( @RequestBody CreateImgPamarsVo createImgPamarsVo) {
//
//		String instanceId = createImgPamarsVo.getInstanceId();
//		String createImg  = phoneService.doCreateImage(new CreateImgPamarsVo()
//				.setInstanceId(instanceId)
//				.setRegionId("cn-hangzhou")
//				.setImageName("company_app")
//				.setDescription("comwithapp")
//		);
//
//		return Result.OK(createImg)  ;
//	}
//
//	@AutoLog(value = "账户管理-公司母盘")
//	@ApiOperation(value="账户管理-公司母盘", notes="账户管理-公司母盘")
//	@RequestMapping(value = "/listImg", method = {RequestMethod.PUT,RequestMethod.POST})
//	public Result<?> listImg( @RequestBody CreateImgPamarsVo createImgPamarsVo) {
//
//		String createImg  = phoneService.doListImages(new CreateImgPamarsVo()
//
//		);
//
//		return Result.OK(createImg)  ;
//	}
//
//	@AutoLog(value = "账户管理-公司母盘")
//	@ApiOperation(value="账户管理-公司母盘", notes="账户管理-公司母盘")
//	@RequestMapping(value = "/deleteM", method = {RequestMethod.PUT,RequestMethod.POST})
//	public Result<?> deleteM( @RequestBody CreateImgPamarsVo createImgPamarsVo) {
//
//		String deleteResult  = phoneService.doDeleteInstances( createImgPamarsVo.getInstanceId());
//
//		return Result.OK(deleteResult)  ;
//	}
//	@AutoLog(value = "账户管理-公司母盘")
//	@ApiOperation(value="账户管理-公司母盘", notes="账户管理-公司母盘")
//	@RequestMapping(value = "/delImg", method = {RequestMethod.PUT,RequestMethod.POST})
//	public Result<?> delImg( @RequestBody ImagePamarsVo imagePamarsVo) {
//
//		String deleteResult  = phoneService.doDeleteImages( imagePamarsVo.getImageId());
//
//		return Result.OK(deleteResult)  ;
//	}


	@AutoLog(value = "账户管理-创建账户登录盘")
	@ApiOperation(value="账户管理-创建账户登录盘", notes="账户管理-创建账户登录盘")
	@RequestMapping(value = "/createLogin", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<?> createLogin( @RequestBody PhoneAccount phoneAccount) {
		phoneAccountTaskService.save(new PhoneAccountTask()
				.setAccountId(phoneAccount.getId())
				.setTaskStatus(TaskEnums.TaskStatus.WAIT.getName())
				.setTaskId(TaskEnums.TaskCode.LOGINP1ID.getName()));


		return Result.OK("操作完成后保存镜像")  ;
	}



	@AutoLog(value = "账户管理-创建账户登录盘")
	@ApiOperation(value="账户管理-创建账户登录盘", notes="账户管理-创建账户登录盘")
	@RequestMapping(value = "/handleCreateLoginSend", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<?> handleCreateLoginSend( @RequestBody PhoneAccount phoneAccount) {
		phoneAccountService.updateById(phoneAccount);
		return Result.OK("")  ;
	}


//
//	/**
//	 * 通过id删除
//	 *
//	 * @param id
//	 * @return
//	 */
//	@AutoLog(value = "账户管理-通过id删除")
//	@ApiOperation(value="账户管理-通过id删除", notes="账户管理-通过id删除")
//	@DeleteMapping(value = "/delete")
//	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
//		phoneAccountService.removeById(id);
//		return Result.OK("删除成功!");
//	}
//
//	/**
//	 * 批量删除
//	 *
//	 * @param ids
//	 * @return
//	 */
//	@AutoLog(value = "账户管理-批量删除")
//	@ApiOperation(value="账户管理-批量删除", notes="账户管理-批量删除")
//	@DeleteMapping(value = "/deleteBatch")
//	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
//		this.phoneAccountService.removeByIds(Arrays.asList(ids.split(",")));
//		return Result.OK("批量删除成功！");
//	}
//
//	/**
//	 * 通过id查询
//	 *
//	 * @param id
//	 * @return
//	 */
//	@AutoLog(value = "账户管理-通过id查询")
//	@ApiOperation(value="账户管理-通过id查询", notes="账户管理-通过id查询")
//	@GetMapping(value = "/queryById")
//	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
//		PhoneAccount phoneAccount = phoneAccountService.getById(id);
//		return Result.OK(phoneAccount);
//	}
//
//  /**
//   * 导出excel
//   *
//   * @param request
//   * @param phoneAccount
//   */
//  @RequestMapping(value = "/exportXls")
//  public ModelAndView exportXls(HttpServletRequest request, PhoneAccount phoneAccount) {
//      return super.exportXls(request, phoneAccount, PhoneAccount.class, "账户管理");
//  }
//
//  /**
//   * 通过excel导入数据
//   *
//   * @param request
//   * @param response
//   * @return
//   */
//  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
//  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
//      return super.importExcel(request, response, PhoneAccount.class);
//  }

}
